package com.wise.domain;

import com.wise.domain.lombok.DemoEntityVo;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;

/**
 * https://github.com/spring-projects/spring-data-examples/tree/master/jpa/jpa21
 */
@Entity
@NoArgsConstructor
@SqlResultSetMapping(
        name = "demoEntityVoMapping",
        classes = @ConstructorResult(
                targetClass = DemoEntityVo.class,
                columns = {
                        @ColumnResult(name = "id", type = Integer.class),
                        @ColumnResult(name = "name", type = String.class)
                }
        )
)
@NamedNativeQuery(
        //DemoEntity表示返回类型，findAllDemoEntityVos为repository方法名
        name = "DemoEntity.findAllDemoEntityVos",
        query = "SELECT id AS id,`name` AS `name` FROM demo_entity",
        //resultSetMapping对应名称为SqlResultSetMapping的name
        resultSetMapping = "demoEntityVoMapping"
)
@Data
public class DemoEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    @Column(length = 15)
    private String name;
    @Column(length = 20)
    private String email;
    @Column(length = 30)
    private String hobby;

    /**
     * 本表demoEntity作为从表，通过外键(JoinColumn的name值)demo_ext_id
     * 去关联demoExt主表的extId(referencedColumnName的值)
     */
    @OneToOne(cascade = CascadeType.PERSIST)
    @JoinColumn(name = "demo_ext_id", referencedColumnName = "extId")
    private DemoExt demoExt;
}
